<template>
  <div class="saveUpdate">
    <el-dialog :title="id?'编辑用户':'新增用户'" :visible.sync="showDialog" @close="$emit('update:showUpdate', false)"
               :showUpdate="showUpdate" width="600px">
      <el-form :model="DetailInfo" ref="saveChn" label-width="120px" size="mini">
        <dialog-wrapper>
          <el-form-item label="用户名:" prop="name"    :rules="[{ required: true, message: '名称不能为空'}]">
            <el-input v-model="DetailInfo.name"   autocomplete="new-password" :disabled="!!id"></el-input>
          </el-form-item>
          <el-form-item v-if="!id" label="用户密码:"    prop="password" :rules="[{ required: true, message: '密码不能为空'}]">
            <el-input v-model="DetailInfo.password"   autocomplete="new-password" type="password"></el-input>
          </el-form-item>
          <el-form-item label="关联角色:" prop="role_id" :rules="[{ required: true, message: '关联角色不能为空'}]">
            <el-select v-model="DetailInfo.role_id" placeholder="请选择" @change="changeRole(DetailInfo.role_id)">
              <el-option v-for="(value, key) in rolelist" :key="key" :label="value" :value="parseInt(key)"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否启用：">
            <el-switch v-model="DetailInfo.enable"></el-switch>
          </el-form-item>
        </dialog-wrapper>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:showUpdate', false)" size="small">取 消</el-button>
        <el-button v-if="id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定</el-button>
        <el-button v-else type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DialogWrapper from '@/components/DialogWrapper'
  import {getUserDetail, saveAddUser, editUserDetail} from '@/api'
  import {useVcode} from "../../utils/CommonJs";

  export default {
    components: {DialogWrapper},
    data() {
      return {
        showDialog: this.showUpdate,
        loading: false,
        DetailInfo: {
          enable: true
        },
        role_id: [],
        accountType: this.$store.state.app.accountType,
        showParent: false,
        Parents: [],
      }
    },
    props: {
      showUpdate: {
        type: Boolean,
        default: false
      },
      id: {
        type: Number
      },
      rolelist: {
        type: Object
      }
    },
    created() {
      if (this.id) {
        this.getDetail()
      }
    },
    methods: {
      changeRole(id) {
        if (id === 3) {
          getParent().then(res => {
            if (res.status === 0) {
              this.Parents = res.data
            }
          })
          this.showParent = true
        } else {
          this.showParent = false
        }
      },
      saveDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {

            saveAddUser(this.DetailInfo).then(res => {
              if (res.status === 0) {
                this.$message({message: '新增成功。', type: 'success'})
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
      updateDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            editUserDetail(this.DetailInfo, this.id).then(res => {
              if (res.status === 0) {
                this.$message({message: '编辑成功。', type: 'success'})
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
      getDetail() {
        this.role_id = []
        getUserDetail(this.id).then(res => {
          if (res.status === 0) {
            this.DetailInfo = res.data
          }
        })
      }
    },
    watch: {
      showUpdate() {
        this.showDialog = this.showUpdate
      }
    }
  }
</script>
